﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;
using Common;
using Entity;
using FredCK.FCKeditorV2;
namespace Web
{
    public partial class SimplePostQuestion : System.Web.UI.Page
    {
        private IList<QuesTypeInfo> QuesTypeInfos = new List<QuesTypeInfo>();
        private static UserLinkExtendInfo Uinfo = new UserLinkExtendInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(base.Request, "SimplePostQuestion"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (!IsPostBack)
            {
                LoadQuesType();
                LoadSubject();
            }
        }

        protected void LoadSubject()
        {
            this.SubjectIdList.Items.Clear();
            IList<SubjectInfo> subjectInfos = new List<SubjectInfo>();
            SubjectIdList.Items.Add(new ListItem("请选择课程", "0"));
            subjectInfos = Subject.GetSubjectInfos();
            foreach (SubjectInfo info in subjectInfos)
            {
                SubjectIdList.Items.Add(new ListItem(info.Name, info.Keyword));
            }
        }

        protected void LoadQuesType()
        {
            QuesTypeInfos = QuesType.GetQuesTypeInfos(0, 0);
            QuesTypeIdList.Items.Clear();
            QuesTypeIdList.Items.Add(new ListItem("请选择试题类型", ""));
            foreach (QuesTypeInfo info in this.QuesTypeInfos)
            {
                QuesTypeIdList.Items.Add(new ListItem(info.Name + "    " + info.Keyword, info.Keyword));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Uinfo = UserValidate.GetInfo(base.Request);
            string str2 = null;
            string str4 = null;
            string str5 = AnswerNew.Value.Trim();
            int quesTypeIdByKeyword = 0;
            int subjectIdByKeyword = 0;
            if (QuesTypeId.Text.Trim() == "")
            {
                Utility.ShowBoxyAlert("必须设定题目类型", this);
            }
            else if (!QuesType.CheckQuesTypeExistByKeyword(QuesTypeId.Text.Trim(), 0))
            {
                Utility.ShowBoxyAlert("题目类型编码错误，请核实", this);
            }
            else
            {
                quesTypeIdByKeyword = QuesType.GetQuesTypeIdByKeyword(QuesTypeId.Text.Trim());
                if (this.SubjectId.Text.Trim() == "")
                {
                    Utility.ShowBoxyAlert("请选择科目", this);
                }
                else if (!Subject.CheckSubjectExistByKeyword(SubjectId.Text.Trim(), 0))
                {
                    Utility.ShowBoxyAlert("科目编码错误，请核实", this);
                }
                else
                {
                    str4 = SubjectId.Text.Trim();
                    subjectIdByKeyword = Subject.GetSubjectIdByKeyword(SubjectId.Text.Trim());
                    if (this.Title.Value.Trim().Length < 10)
                    {
                        Utility.ShowBoxyAlert("题目内容过短", this);
                    }
                    else
                    {
                        str2 = this.Title.Value.Trim().Replace("\"../../upfile/", "\"upfile/");
                        QuestionInfo questionInfo = new QuestionInfo();
                        questionInfo.Answer = str5;
                        questionInfo.CognitiveLevelId = CognitiveLevel.GetFirstId();
                        questionInfo.BookId = 0;
                        questionInfo.DifficId = Diffic.GetFirstId();
                        questionInfo.KeyWord = "";
                        questionInfo.KnowId = Knowledge.GetFirstId();
                        questionInfo.LevelId = Level.GetFirstId();
                        questionInfo.PrintDate = DateTime.Now;
                        questionInfo.SubjectId = subjectIdByKeyword;
                        questionInfo.ComputeTime = 0;
                        questionInfo.SubKnow = "";
                        questionInfo.ROC = "";
                        questionInfo.PrinterId = Uinfo.Id;
                        questionInfo.Printer = Uinfo.Name;
                        questionInfo.QuesTypeId = quesTypeIdByKeyword;
                        questionInfo.Score = 0M;
                        questionInfo.State = 4;
                        questionInfo.StatusId = 4;
                        questionInfo.SubjectId = subjectIdByKeyword;
                        questionInfo.Title = str2;
                        questionInfo.Serial = "no";
                        questionInfo.UserId = Uinfo.Id;
                        questionInfo.Remark = "";
                        questionInfo.CreateDate = DateTime.Now;
                        Question.AddQuestion(questionInfo);
                        Utility.ShowBoxyConfirm("添加成功，是否清除已有选项？", "SimplePostQuestion.aspx", this);
                        this.Title.Value = "";
                        this.AnswerNew.Value = "";
                    }
                }
            }
        }
    }
}
